package q1.project.api.admin.controller.system.role;

// TODO: 替换为自定义注解
// import q1.project.web.annotation.RequireAuth;
// import q1.project.web.annotation.RequirePermission;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import q1.project.core.base.BaseController;
import q1.project.core.result.Result;
import q1.project.log.OperationLog;
import q1.project.modules.system.role.entity.RoleEntity;
import q1.project.modules.system.role.model.RoleVm;
import q1.project.modules.system.role.service.RoleService;

/**
 * 角色管理控制器
 *
 * @author Q1 Team
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/admin/sys/roles")
@RequiredArgsConstructor
// TODO: @RequireAuth
public class AdminRoleController extends BaseController {

  private final RoleService roleService;

  /** 创建角色 */
  @PostMapping("/create")
  // TODO: @RequirePermission("role:create")
  @OperationLog(module = "角色管理", description = "创建角色")
  public Result<RoleVm.RoleResponse> createRole(@RequestBody RoleEntity role) {
    log.info("创建角色请求: roleCode={}", role.getRoleCode());
    RoleVm.RoleResponse createdRole = roleService.createRole(role);
    return Result.success(createdRole);
  }

  /** 更新角色 */
  @PostMapping("/update/{id}")
  // TODO: @RequirePermission("role:update")
  @OperationLog(module = "角色管理", description = "更新角色")
  public Result<RoleVm.RoleResponse> updateRole(
      @PathVariable String id, @RequestBody RoleEntity role) {
    log.info("更新角色请求: id={}", id);
    role.setId(id);
    RoleVm.RoleResponse updatedRole = roleService.updateRole(role);
    return Result.success(updatedRole);
  }

  /** 删除角色 */
  @PostMapping("/delete/{id}")
  // TODO: @RequirePermission("role:delete")
  @OperationLog(module = "角色管理", description = "删除角色")
  public Result<Void> deleteRole(@PathVariable String id) {
    log.info("删除角色请求: id={}", id);
    roleService.deleteRole(id);
    return Result.success();
  }

  /** 查询角色详情 */
  @GetMapping("/{id}")
  // TODO: @RequirePermission("role:view")
  public Result<RoleVm.RoleResponse> getRoleById(@PathVariable String id) {
    log.info("查询角色详情: id={}", id);
    RoleVm.RoleResponse role = roleService.getRoleById(id);
    return Result.success(role);
  }

  /** 分页查询角色列表 */
  @GetMapping
  // TODO: @RequirePermission("role:list")
  public Result<Page<RoleVm.RoleResponse>> listRoles(RoleVm.RoleQuery query) {
    query = handlePageQuery(query);
    logQuery("分页查询角色", query);
    Page<RoleVm.RoleResponse> roles = roleService.listRoles(query);
    return Result.success(roles);
  }

  /** 查询所有启用的角色 */
  @GetMapping("/enabled")
  public Result<List<RoleVm.RoleResponse>> listEnabledRoles() {
    log.info("查询所有启用的角色");
    List<RoleVm.RoleResponse> roles = roleService.listEnabledRoles();
    return Result.success(roles);
  }

  /** 分配权限 */
  @PostMapping("/{id}/permissions")
  // TODO: @RequirePermission("role:assign-permissions")
  @OperationLog(module = "角色管理", description = "分配权限")
  public Result<Void> assignPermissions(
      @PathVariable String id, @RequestBody Set<String> permissionIds) {
    log.info("分配角色权限: id={}, permissionIds={}", id, permissionIds);
    roleService.assignPermissions(id, permissionIds);
    return Result.success();
  }

  /** 分配菜单 */
  @PostMapping("/{id}/menus")
  // TODO: @RequirePermission("role:assign-menus")
  @OperationLog(module = "角色管理", description = "分配菜单")
  public Result<Void> assignMenus(@PathVariable String id, @RequestBody Set<String> menuIds) {
    log.info("分配角色菜单: id={}, menuIds={}", id, menuIds);
    roleService.assignMenus(id, menuIds);
    return Result.success();
  }
}
